package com.example.demo.service;

import com.example.demo.bean.CmsLink;
import com.example.demo.mapper.CmsLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class CmsLinkService {
    @Autowired
    CmsLinkMapper linkMapper;

    public boolean saveOrUpdate(CmsLink link) {
            if (link.getId() != null) {
                linkMapper.updateByPrimaryKey(link);
                return true;
            } else {
                Random r =new Random();
                int i= r.nextInt(100000000);
                link.setId(i);
                linkMapper.insert(link);
                return true;
            }
    }
    public List<CmsLink> all(){
        return linkMapper.selectAll();
    }
    public int delete(int  id){
        int i=linkMapper.deleteByPrimaryKey(id);
        return i;
    }
    public String deleteByIds(int[] ids) {
        ArrayList<String> suc = new ArrayList<String>();
        ArrayList<String> noExist = new ArrayList<String>();
        if (ids != null && ids.length > 0) {
            for (int id : ids) {
                int i = delete(id);
                if (i == 0) {
                    noExist.add(String.valueOf(id));
                }
                if (i == 1) {
                    suc.add(String.valueOf(id));
                }
            }
            String str = "success:";
            if (suc.size() > 0) {
                str += "成功删除了" + suc + "!";
            }
            if (noExist.size() > 0) {
                str += "链接" + noExist + "不存在";
            }
            return str;
        } else {
            return "请输入要删除的链接编号";
        }
    }
}
